package com.hulu.yec.teach.testpaper.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.hulu.yec.common.Constant;
import com.hulu.yec.common.PageModel;
import com.hulu.yec.teach.testpaper.entity.PaperConditionEntity;
import com.hulu.yec.teach.testpaper.entity.TestPaperEntity;
import com.hulu.yec.teach.testpaper.service.PaperAddDao;
import com.hulu.yec.teach.testpaper.service.PaperDetailDao;
import com.hulu.yec.teach.testpaper.service.TestPeperDao;

@WebServlet("/teach/testpaper/TestPaperServlet")
public class TestPaperServlet<TesPaperEntity> extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
	private static final TestPeperDao testPeperDao = new TestPeperDao();  
	private static final PaperDetailDao paperDetailDao = new PaperDetailDao();
	private static final PaperAddDao paperAddDao = new PaperAddDao();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constant.ENCODING_UTF8);
		String command = request.getParameter("command");
		if("list".equals(command)){
			try {
				list(request,response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if("detail".equals(command)){
			detail(request,response);
		}else if("changeState".equals(command)){
			changeState(request,response);
		}else if("paperAdd".equals(command)){
			paperAdd(request,response);
		}else if("prePaperAdd".equals(command)){
			prePaperAdd(request,response);
		}
	}
	/**
	 * 试卷分页条件查询
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 * @throws SQLException 
	 */
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		//接受前台传来的参数
		PaperConditionEntity conditionEntity = new PaperConditionEntity();
		int pageNo =1;// Constant.DEFAULT_PAGE_NO;
		int pageSize =5;// Constant.DEFAULT_PAGE_SIZE;
		String pageNoStr = request.getParameter("pageNo");
		if (StringUtils.isNoneBlank(pageNoStr)) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		String name = request.getParameter("name");
		String courseName = request.getParameter("courseName");
		String state = request.getParameter("state");
		if(StringUtils.isNotBlank(name)){
			conditionEntity.setName(name);
		}
		
		if(StringUtils.isNoneBlank(courseName)){
			conditionEntity.setCourseName(courseName);
		}
		if(state != null && Integer.parseInt(state)!=0){
			conditionEntity.setState(Integer.parseInt(state));
		}
		   conditionEntity.setCourseNameList(testPeperDao.getAllCourseName());
		//调用业务
	    PageModel<TestPaperEntity> pageModel = testPeperDao.list(conditionEntity,pageSize,pageNo);
	    //转向
		request.setAttribute("conditionEntity", conditionEntity);
		request.setAttribute("pageModel", pageModel);
		request.getRequestDispatcher("/WEB-INF/view/teach/testpaper/testpaper-list.jsp").forward(request, response);
	}
	
	 /**
	    * 试卷详细
	    * @param request
	    * @param response
	    * @throws IOException 
	    * @throws ServletException 
	    */
		private void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  //获取前台传来参数
			int id = Integer.parseInt(request.getParameter("id"));
			TestPaperEntity testPaper  = paperDetailDao.detail(id);
			//转向
			request.setAttribute("testPaper", testPaper);
			request.getRequestDispatcher("/WEB-INF/view/teach/testpaper/paper-detail.jsp").forward(request, response);
		}

		/**
		 * 新增前跳转并查询出所有课程
		 * @param request
		 * @param response
		 * @throws IOException 
		 * @throws ServletException 
		 */
		private void prePaperAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			//调用业务
			List<TestPaperEntity> condition = paperAddDao.list();
			//转向
			request.setAttribute("condition", condition);
			request.getRequestDispatcher("/WEB-INF/view/teach/testpaper/paper-add.jsp").forward(request, response);
		}
		
		/**
		 * 试卷新增
		 * @param request
		 * @param response
		 * @throws IOException 
		 * @throws ServletException 
		 */
		private void paperAdd(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
		 
			TestPaperEntity testPaperEntity = new TestPaperEntity();
			
			//获取前台的参数并封装在TestPaperEntity中
			String name = request.getParameter("name");
			testPaperEntity.setName(name);
			
		    int paperType = Integer.parseInt(request.getParameter("paperType"));
		    testPaperEntity.setPaperType(paperType);
		    
		    int courseId = Integer.parseInt(request.getParameter("courseName"));
		    testPaperEntity.setCourseId(courseId);
		   
		    int totalPoint =Integer.parseInt(request.getParameter("totalPoint")); 
		    testPaperEntity.setTotalPoint(totalPoint);
		   
		    int passPoint = Integer.parseInt(request.getParameter("passPoint"));
		    testPaperEntity.setPassPoint(passPoint);
		  
		    int totalMinutes = Integer.parseInt(request.getParameter("totalMinutes"));
		    testPaperEntity.setTotalMinutes(totalMinutes);
		    
		    int singleOptionNumber = Integer.parseInt(request.getParameter("singleOptionNumber"));
		    testPaperEntity.setSingleOptionNumber(singleOptionNumber);
		  
		    int singleOptionEachPoint = Integer.parseInt(request.getParameter("singleOptionEachPoint"));
		    testPaperEntity.setSingleOptionEachPoint(singleOptionEachPoint);
		   
		    int singleOptionDifficulty = Integer.parseInt(request.getParameter("singleOptionDifficulty"));
		    testPaperEntity.setSingleOptionDifficulty(singleOptionDifficulty);
		   
		    int multiOptionNumber = Integer.parseInt(request.getParameter("multiOptionNumber"));
		    testPaperEntity.setMultiOptionNumber(multiOptionNumber);
		    
		    int multiOptionEachPoint = Integer.parseInt(request.getParameter("multiOptionEachPoint"));
		    testPaperEntity.setMultiOptionEachPoint(multiOptionEachPoint);
		    
		    int multiOptionDifficulty = Integer.parseInt(request.getParameter("multiOptionDifficulty"));
		    testPaperEntity.setMultiOptionDifficulty(multiOptionDifficulty);
		    
		    int judgeNumber = Integer.parseInt(request.getParameter("judgeNumber"));
		    testPaperEntity.setJudgeNumber(judgeNumber);
		    
		    int judgeEachPoint = Integer.parseInt(request.getParameter("judgeEachPoint"));
		    testPaperEntity.setJudgeEachPoint(judgeEachPoint);
		    
		    int judgeOptionDifficulty = Integer.parseInt(request.getParameter("judgeOptionDifficulty"));
		    testPaperEntity.setJudgeDifficulty(judgeOptionDifficulty);
		    
		    String memo = request.getParameter("memo");
		    testPaperEntity.setMemo(memo);
		    int row = new PaperAddDao().add(testPaperEntity);
		  
		  //转向
		    if(row > 0){
		    	response.sendRedirect("TestPaperServlet?command=list");
		    } else {
		    	request.getRequestDispatcher("/teach/testpaper/TestPaperServlet?command=prepaperAdd").forward(request, response);
		    }
		   
		}
	/**
	 * 获取id 状态更新	
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
		private void changeState(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
			String inStr = request.getParameter("id");
			int id = 0;
			if (StringUtils.isNoneBlank(inStr)) {
				id = Integer.parseInt(inStr);
			} else {
				throw new RuntimeException("错误请求");
			}
			int reState = testPeperDao.getState(id);
			if (reState == 1) {
				reState = 2;
			} else if (reState == 2) {
				reState = 1;
			}
			testPeperDao.updateState(id, reState);
			// 转向
			response.setCharacterEncoding(Constant.ENCODING_UTF8);
			PrintWriter out = response.getWriter();
			int state = testPeperDao.getState(id);
			out.print(state);
			out.close();
		}
}